﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Com.Core.Helps
{
    /// <summary>
    /// Guid辅助方法
    /// </summary>
    public class GuidHelper
    {
        /// <summary>
        /// 创建comb型Guid
        /// </summary>
        /// <returns></returns>
        public static Guid GenerateComb()
        {
            byte[] guidArray = Guid.NewGuid().ToByteArray();

            DateTime baseDate = new DateTime(1900, 1, 1);
            DateTime now = DateTime.Now;

            // Get the days and milliseconds which will be used to build    
            //the byte string    
            TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
            TimeSpan msecs = now.TimeOfDay;
            // Convert to a byte array        
            // Note that SQL Server is accurate to 1/300th of a    
            // millisecond so we divide by 3.333333    
            byte[] daysArray = BitConverter.GetBytes(days.Days);
            byte[] msecsArray = BitConverter.GetBytes((long)
              (msecs.TotalMilliseconds / 3.333333));
            // Reverse the bytes to match SQL Servers ordering    
            Array.Reverse(daysArray);
            Array.Reverse(msecsArray);

            // Copy the bytes into the guid    
            Array.Copy(daysArray, daysArray.Length - 2, guidArray,
              guidArray.Length - 6, 2);
            Array.Copy(msecsArray, msecsArray.Length - 4, guidArray,
              guidArray.Length - 4, 4);

            return new Guid(guidArray);
        }

        /// <summary>
        /// 根据GUID获取19位的唯一数字序列
        /// </summary>
        public static long GuidToLongId(Guid guid)
        {
            byte[] buffer = guid.ToByteArray();
            return BitConverter.ToInt64(buffer, 0);
        }

        /// <summary>
        /// 判断输入字符串是否为合法Guid
        /// </summary>
        /// <param name="str"></param>
        /// <param name="isEncrypt">输入是否加密过</param>
        /// <returns></returns>
        public static bool IsGuid(string str, bool isEncrypt = false)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (isEncrypt) str = EncryptHelper.AES.Decrypt(str);
            try
            {
                new Guid(str);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 判断数组是否为合法Guid
        /// </summary>
        /// <param name="strs"></param>
        /// <param name="isEncrypt">输入是否加密</param>
        /// <returns></returns>
        public static bool IsGuid(string[] strs, bool isEncrypt = false)
        {
            if (null == strs || !strs.Any()) return false;
            return strs.All(str => IsGuid(str, isEncrypt));
        }

        /// <summary>
        /// 将字符串数组转成Guid数组,如果字符串不合法则返回null
        /// </summary>
        /// <param name="strs">输入字符串数组</param>
        /// <param name="isEncrypt">是否加密</param>
        /// <returns></returns>
        public static Guid[] ConvertStringsToGuids(string[] strs, bool isEncrypt = false)
        {
            if (IsGuid(strs, isEncrypt))
            {
                int length = strs.Length;
                Guid[] retVal = new Guid[length];
                for (int i = 0; i < length; i++)
                {
                    retVal[i] = new Guid(isEncrypt ? EncryptHelper.AES.Decrypt(strs[i]) : strs[i]);
                }
                return retVal;
            }
            return null;
        }

        public static string ConvetToString(List<Guid> guids)
        {
            string retVal = guids.Aggregate(string.Empty, (current, guid) => current + ("'" + guid + "',"));
            return retVal.TrimEnd(',');
        }
    }
}
